﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

public partial class admin_Articles_List : System.Web.UI.Page
{
  CategoryBLL categoryData = new CategoryBLL();
  ArticleBLL articleData = new ArticleBLL();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;

      LoadCategory();
      ShowData();
    }
  }

  private void ShowChild(int parentId, string space)
  {
    List<Category> listDataSource = categoryData.GetByParent(parentId);

    foreach (Category item in listDataSource)
    {
      ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
      dropParent.Items.Add(obj);

      ShowChild(item.Id, space + "- - - ");
    }
  }
  private void LoadCategory()
  {
    dropParent.Items.Clear();
    ShowChild(0, " - - ");
    ListItem itemRoot = new ListItem("Danh mục gốc", "0");
    dropParent.Items.Insert(0, itemRoot);

    ListItem itemNull = new ListItem("Tất cả", "-1");
    dropParent.Items.Insert(0, itemNull);

    if (Session["parent"] != null)
      dropParent.SelectedValue = Session["parent"].ToString();
  }
  private void ShowData()
  {
    int parentId = Convert.ToInt32(dropParent.SelectedValue);
    int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
    gridView.PageSize = PageRow;
    if (dropIsEnable.SelectedIndex == 0)
    {
      if (parentId != -1)
        gridView.DataSource = articleData.GetByCategory(parentId);
      else
        gridView.DataSource = articleData.GetAll();
    }
    else
    {
      bool isEnable = true;
      if (dropIsEnable.SelectedValue == "2") isEnable = false;

      if (parentId != -1)
        gridView.DataSource = articleData.GetByCategory(parentId, isEnable);
      else
        gridView.DataSource = articleData.GetAll(isEnable);
    }
    gridView.DataBind();

  }
  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[2].Text);
            articleData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      if (e.Row.RowIndex >= 0) //Bo qua dong dau
      {       
        int id = Convert.ToInt32(e.Row.Cells[2].Text);
        if (e.Row.Cells[4].Text == "0") e.Row.Cells[4].Text = "Danh mục gốc";
        else e.Row.Cells[4].Text = categoryData.GetById(Convert.ToInt32(e.Row.Cells[4].Text)).Name;

        e.Row.Cells[6].Text = Convert.ToDateTime(e.Row.Cells[6].Text).ToShortDateString();
        bool isEnable = articleData.GetById(id).isEnable;

        //Icon check
        Image img = new Image();
        foreach (Control item in e.Row.Cells[8].Controls)
        {
          if (item.GetType() == typeof(Image))
          {
            img = (Image)item;
            break;
          }
        }
        if (isEnable)
          img.ImageUrl = "~/Images/check.png";
        else
        {
          img.ImageUrl = "~/Images/an.png";

        }
      }
    }
    catch { }
  }
  protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    try
    {
      lbThongBao.Text = "";
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text =  "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
      string strImage = articleData.GetById(id).Image;
      //delete image
      Myfunction.DeleteFile("Images/Article", strImage);
      articleData.Delete(id);      
      Myfunction.ThongBao(lbThongBao, "Bài viết đã được xóa");

      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void btnVisible_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    lbThongBao.Text = "";
    Enable(false);
  }
  protected void btnEnable_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    lbThongBao.Text = "";
    Enable(true);
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[1].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[2].Text);
              //Delete image
              string strImage = articleData.GetById(id).Image;
              Myfunction.DeleteFile("Images/Article", strImage);

              articleData.Delete(id);
            }
            break;
          }
        }
      }
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
    ShowData();
  }
  protected void btnAdd_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Articles/Details.aspx");
  }
}